package com.zs.myorm.framework.core.method.impl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import com.zs.myorm.framework.core.execute.ISqlExecute;
import com.zs.myorm.framework.core.method.IMethodHandler;
import com.zs.myorm.framework.core.method.IMethodHandlerStrategyContext;

public class MethodHandlerStrategyContextImpl implements IMethodHandlerStrategyContext {
	private Map<String, IMethodHandler> methods = new java.util.concurrent.ConcurrentHashMap<String, IMethodHandler>();
	private ISqlExecute sqlExecute;
	
	public MethodHandlerStrategyContextImpl() {
		init();
	}
	private void init() {
		
	}
	public void setSqlExecute(ISqlExecute sqlExecute) {
		this.sqlExecute = sqlExecute;
	}
	

	@Override
	public Object invoke(Class<?> beanClass, Method method, Object[] args)throws Exception {
		Annotation[] annotations = method.getAnnotations();
		if (annotations == null || annotations.length == 0) {
			
			IMethodHandler methodHandler = methods.get(method.getName());
			methodHandler.setSqlExecute(sqlExecute);
			return methodHandler.invoke(beanClass, method, args);
		}
		return null;
	}
	public void setMethodHandlers(List<IMethodHandler> methodHandlers) {
		for(IMethodHandler handler:methodHandlers) {
			methods.put(handler.getMethodName(), handler);
		}
	}

}
